package com.example.test.activity;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;




















import com.example.test.R;
import com.example.test.R.id;
import com.example.test.R.layout;
import com.example.test.activity.PullDownView.OnPullDownListener;
import com.example.test.model.Constants;
import com.example.test.model.FormatTools;
import com.example.test.model.ImageFactory;
import com.example.test.model.StatusTextProcess;
import com.example.test.model.imgdown.AsyncDownHeadImageTask;
import com.example.test.model.imgdown.AsyncDownHeadImgTask;
import com.example.test.model.imgdown.AsyncDownStatusImageTask;
import com.example.test.model.json_analyzer.HashDataProcessor;
import com.example.test.model.social_api.NetAPI;
import com.example.test.model.social_api.QQAPI;
import com.example.test.model.social_api.RennAPI;
import com.example.test.model.social_api.SinaAPI;
import com.example.test.model.social_api.SohuWeiboAPI;
import com.tencent.weibo.sdk.android.api.util.Util;
import com.tencent.weibo.sdk.android.model.GlobalVar;

import android.R.drawable;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class HomeActivity extends Activity implements OnPullDownListener{

	String uid;
	String name;
	String eQQTimeStamp="";
	String sinaMaxId="";
	String netMaxId="";
	String sohuMaxId="";
	String activity_type="";
	Bitmap loading=null;
	private ListView lvTimeline=null;
    private PullDownView mPullDownView;
	HomeTimeLineAdapter	adapter=null;
	List<HashMap<String,String>> lArrTimeline=null;
	 HashMap<String, List<byte[]>> mapImgList=new HashMap<String, List<byte[]>>();
	Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch(msg.what)
			{
			case Constants.MSG_GETTIMELINE_SUCCESS:
				setStatusLV(msg.obj);break;
			case Constants.MSG_GETUSERINFO_SUCCESS:
				setUI(msg.obj);break;
			case Constants.MSG_GETUSERINFO_FAIL:Toast.makeText(HomeActivity.this, "加载用户信息失败", 1000).show();HomeActivity.this.finish();break;
			case Constants.MSG_REPOST_SUCCESS:
			{
				Toast.makeText(HomeActivity.this, "转发成功", 1000).show();
				break;
			}
			case Constants.MSG_REPOST_FAIL:
			{
				Toast.makeText(HomeActivity.this, "转发失败:"+msg.obj, 1000).show();
				
				break;
			}
			case Constants.MSG_COMMENT_SUCCESS:
			{
				Toast.makeText(HomeActivity.this, "评论成功", 1000).show();
				break;
			}
			case Constants.MSG_COMMENT_FAIL:
			{
				Toast.makeText(HomeActivity.this, "评论失败:"+msg.obj, 1000).show();
				break;
			}
			case Constants.MSG_DOWNIMG_SUCCESS:
				HashMap<String, Object> map1=(HashMap<String, Object>)msg.obj;
				setUserHead((Drawable)map1.get("img"));
				break;
			case Constants.MSG_DOWNSTATUSIMG_SUCCESS:
			{
				HashMap<String, Object> map=(HashMap<String, Object>)msg.obj;
				String id=(String)map.get("id");
				String [] temp=id.split("_");
				mapImgList.get(temp[0]+"_"+temp[1]).set(Integer.parseInt(temp[2]), (byte[])map.get("img"));
				adapter.notifyDataSetChanged();
			}
			}
			super.handleMessage(msg);
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		GlobalVar.changed=true;
		 WindowManager wm =getWindowManager();
	    	int width = wm.getDefaultDisplay().getWidth();
	    	loading=BitmapFactory.decodeResource(getResources(), R.drawable.loading);
	    	loading=ImageFactory.scaleImg(width/3-10, width/3-10, loading);
	   
	}
	
	@Override
	protected void onResume() {
		if(GlobalVar.changed)
		{
			if(lArrTimeline!=null)
			{
			lArrTimeline.clear();
			adapter.notifyDataSetChanged();
			}
			 mPullDownView = (PullDownView) findViewById(R.id.pull_down_view);
		        mPullDownView.setOnPullDownListener(this);
		        lvTimeline = mPullDownView.getListView();
			Intent intent=getIntent();
			Bundle bundle = intent.getExtras();
			activity_type=bundle.getString("activity_type");
			RelativeLayout rlUser=(RelativeLayout)findViewById(R.id.rlUser);
			rlUser.setVisibility(View.VISIBLE);
			if(activity_type.equals("home"))
			{
				if(GlobalVar.enWhich.equals("all"))
				{
					rlUser.setVisibility(View.GONE);
				}
				
				SinaAPI.getUserTimeline(handler, GlobalVar.sinaUid, "0", 5);
				QQAPI.getUserline(this, Util.getSharePersistent(this, "OPEN_ID"),null, handler,"0",5);
				NetAPI.getUserline("",null, handler,"0",5);
				SohuWeiboAPI.getUserline("",null, handler,"0",5);
				if(!GlobalVar.enWhich.equals("all"))
				{
					if(GlobalVar.enWhich.equals("sina"))
					{
						SinaAPI.getUserInfo(handler, GlobalVar.sinaUid, "");
					}
					else if(GlobalVar.enWhich.equals("QQ"))
					{
						QQAPI.getUserInfo(this, Util.getSharePersistent(this, "OPEN_ID"), null, handler);
					}
					else if(GlobalVar.enWhich.equals("netease"))
					{
						NetAPI.getUserInfo("", "", handler);
					}
					else if(GlobalVar.enWhich.equals("sohu"))
					{
						SohuWeiboAPI.getUserInfo("", "", handler);
					}
				}
			}
			else
			{
		    uid =bundle.getString("uid");
		    name = bundle.getString("name");
		    if(activity_type.equals("sina"))
		    {
		    	SinaAPI.getUserInfo(handler, uid,name);
		    }
		    else if(activity_type.equals("QQ"))
		    {
		    	QQAPI.getUserline(this, uid,name, handler,"0",5);
		    	QQAPI.getUserInfo(this, uid,name, handler);
		    }
		    else if(activity_type.equals("Renn"))
		    {
		    	Toast.makeText(this, "人人暂时不支持该功能！", 1000).show();
		    }
		    else if(activity_type.equals("netease"))
		    {
		    	NetAPI.getUserInfo(uid,name, handler);
		    	NetAPI.getUserline(uid,name, handler,"0",5);
		    }
		    else if(activity_type.equals("sohu"))
		    {
		    	SohuWeiboAPI.getUserInfo(uid,name, handler);
		    	SohuWeiboAPI.getUserline(uid,name, handler,"0",5);
		    }
			}
			GlobalVar.changed=false;
		}
		super.onResume();
	}

	@Override
    public void onRefresh() {
		 if(activity_type.equals("home"))
		 {
			 SinaAPI.getUserTimeline(handler, GlobalVar.sinaUid,"0",5);
				QQAPI.getUserline(this, Util.getSharePersistent(this, "OPEN_ID"),null, handler,"0",5);
				NetAPI.getUserline("",null, handler,"0",5);
				SohuWeiboAPI.getUserline("",null, handler,"0",5);
		
		 }
		 else if(activity_type.equals("QQ"))
		 {
			 QQAPI.getUserline(this, uid,name, handler,"0",5);
		 }
		 else if(activity_type.equals("netease"))
		 {
			 NetAPI.getUserline(uid,name, handler,"0",5);
		 }
		 else if(activity_type.equals("sohu"))
		 {
			 SohuWeiboAPI.getUserline(uid,name, handler,"0",5);
		 }
		 lArrTimeline.clear();
    }
 
    @Override
    public void onMore() {
    	if(activity_type.equals("home"))
    	{
    	if(!eQQTimeStamp.equals(""))
    		QQAPI.getUserline(this, Util.getSharePersistent(this, "OPEN_ID"),null, handler,eQQTimeStamp,5);
    	else
    		QQAPI.getUserline(this, Util.getSharePersistent(this, "OPEN_ID"),null, handler,"0",5);
    	if(!sinaMaxId.equals(""))
    		SinaAPI.getUserTimeline(handler, GlobalVar.sinaUid, sinaMaxId, 5);
    	else
    		SinaAPI.getUserTimeline(handler, GlobalVar.sinaUid, "0", 5);
    	if(!netMaxId.equals(""))
 	       NetAPI.getUserline("",null, handler, netMaxId, 5);
 	   else
 	       NetAPI.getUserline("",null, handler, "0", 5);
    	if(!sohuMaxId.equals(""))
  	       SohuWeiboAPI.getUserline("",null, handler, sohuMaxId, 5);
  	   else
  		   SohuWeiboAPI.getUserline("",null, handler, "0", 5);
    	}
    	else if(activity_type.equals("QQ"))
		 {
    		if(!eQQTimeStamp.equals(""))
        		QQAPI.getUserline(this, uid,name, handler,eQQTimeStamp,5);
        	else
        		QQAPI.getUserline(this, uid,name, handler,"0",5);
		 }
		 else if(activity_type.equals("netease"))
		 {
			 if(!netMaxId.equals(""))
		 	       NetAPI.getUserline(uid,name, handler, netMaxId, 5);
		 	   else
		 	       NetAPI.getUserline(uid,name, handler, "0", 5);
		 }
		 else if(activity_type.equals("sohu"))
		 {
			 if(!sohuMaxId.equals(""))
		  	       SohuWeiboAPI.getUserline(uid,name, handler, sohuMaxId, 5);
		  	   else
		  		   SohuWeiboAPI.getUserline(uid,name, handler, "0", 5);
		 }
    	
    }
	public void setUI(Object obj)
	{
		final List<HashMap<String, String>> lArrHashData=(List<HashMap<String, String>>) obj;
		Thread thread=new Thread(new AsyncDownHeadImgTask(handler,"0",lArrHashData.get(0).get("avatar_large")));
		thread.start();
		TextView tvUserName=(TextView)findViewById(R.id.tvUserName);
		TextView tvStatusCount=(TextView)findViewById(R.id.tvStatusCount);
		TextView tvFollow=(TextView)findViewById(R.id.tvFollow);
		TextView tvFollower=(TextView)findViewById(R.id.tvFollower);
		tvUserName.setText(lArrHashData.get(0).get("name"));
		tvFollower.setText("粉丝（"+lArrHashData.get(0).get("followers_count")+"）");
		tvFollow.setText("关注（"+lArrHashData.get(0).get("friends_count")+"）");
		tvStatusCount .setText("微博（"+lArrHashData.get(0).get("statuses_count")+"）");
		tvFollow.setOnClickListener(new TextView.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(!activity_type.equals("home")||GlobalVar.enWhich.equals("sohu"))
				{
					return;
				}
				
				Intent intent=new Intent();
				intent.setClass(HomeActivity.this, FollowListActivity.class);
				intent.putExtra("uid", GlobalVar.sinaUid);
				startActivity(intent);
			}
		});
        tvFollower.setOnClickListener(new TextView.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(!activity_type.equals("home")||GlobalVar.enWhich.equals("sohu"))
				{
					return;
				}
				
				Intent intent=new Intent();
				intent.setClass(HomeActivity.this, FollowerListActivity.class);
				intent.putExtra("uid", GlobalVar.sinaUid);
				startActivity(intent);
				
			}
		});
        
	}
	private void judgeOnMoreFirst(List<HashMap<String,String>> tempList)
	{
		String resource=tempList.get(0).get("resource");
		String id=tempList.get(0).get("id");
		if((resource.equals("sina")&&id.equals(sinaMaxId))||(resource.equals("sohu")&&id==sohuMaxId)||(resource.equals("netease")&&id==netMaxId))
	    tempList.remove(0);
	}
	public void setUserHead(Drawable drawable)
	{
		ImageView ivUserHead=(ImageView)findViewById(R.id.ivUserHead);
		ivUserHead.setImageDrawable(drawable);
	}
	
	public void setStatusLV(Object obj)
	{
		mPullDownView.notifyDidLoad();
		mPullDownView.notifyDidRefresh();
		mPullDownView.notifyDidMore();
		List<HashMap<String,String>> tempList =(List<HashMap<String,String>>)obj;
		HashDataProcessor.timelineDataProcess(tempList);

		if(tempList.size()!=0)
		{
			judgeOnMoreFirst(tempList);
		}
		setPageInfo(tempList);
		startDownStatusImg(tempList);
		if(lArrTimeline==null)
		{
		lArrTimeline=tempList;
		}
		else
		{
		lArrTimeline.addAll(tempList);
		}
		Collections.sort(lArrTimeline,new StatusListComparator());
		if(adapter==null)
		{
			adapter=new HomeTimeLineAdapter(HomeActivity.this,handler, lArrTimeline);
		    lvTimeline.setAdapter(adapter);
		}
		else
			adapter.notifyDataSetChanged();
		
        mPullDownView.enableAutoFetchMore(true, 1);
       
          
	 }
	
	private void setPageInfo(List<HashMap<String,String>> tempList)
	{
		if(tempList==null||tempList.size()==0)
			return;
		if(tempList.get(0).get("resource").equals("QQ"))
		{
			eQQTimeStamp=tempList.get(tempList.size()-1).get("timestamp");
		}
		else if(tempList.get(0).get("resource").equals("sina"))
		{
			sinaMaxId=tempList.get(tempList.size()-1).get("id");
		}
		else if(tempList.get(0).get("resource").equals("netease"))
		{
			netMaxId=tempList.get(tempList.size()-1).get("cursor_id");
		}
		else if(tempList.get(0).get("resource").equals("sohu"))
		{
			sohuMaxId=tempList.get(tempList.size()-1).get("id");
		}
	}
	private void startDownStatusImg(List<HashMap<String,String>> tempList)
	 {
		 int i=0;
			for (HashMap<String, String> hashMap : tempList) {
				String id=tempList.get(i).get("resource")+"_"+tempList.get(i).get("id");
				List<byte[]> imgList=new LinkedList<byte[]>();
			String images=hashMap.get("images");
				if(!images.equals(""))
				{
					String []arrUrl=images.split(",");
					
					int j=0;
					for (String string : arrUrl) {
						byte[] drawable=null;
						imgList.add(drawable);
			Thread thread=new Thread(new AsyncDownStatusImageTask(handler, id+"_"+j,string));
			thread.start();
						j++;
					}
					
					
				}
				
				mapImgList.put(id, imgList);
				i++;
			}
	 }
	
	

class HomeTimeLineAdapter extends BaseAdapter
{
 Context context;
 List<HashMap<String, String>> lsHashData;
 Handler handler;
 public HomeTimeLineAdapter(Context context,Handler handler,List<HashMap<String, String>> data)
 {
	  this.handler=handler;
	  this.context=context;
	  this.lsHashData=data;
 }
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lsHashData.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		{
			Log.v("count", position+"");
			if(convertView==null)
			{
	      	convertView=LayoutInflater.from(context).inflate
			    (R.layout.home_statusitem,null);
			}
			
		TextView tvstatustext=(TextView)convertView.findViewById(R.id.tvstatustext);
		TextView tvcretime=(TextView)convertView.findViewById(R.id.tvcretime);
		TextView tvsource=(TextView)convertView.findViewById(R.id.tvsource);
		final TextView tvstatusattitude=(TextView)convertView.findViewById(R.id.tvstatusattitude);
		TextView tvstatuscomment=(TextView)convertView.findViewById(R.id.tvstatuscomment);
		TextView tvstatusreposts=(TextView)convertView.findViewById(R.id.tvstatusreposts);
		TextView tvretstatustext=(TextView)convertView.findViewById(R.id.tvretstatustext);
		tvcretime.setText(lsHashData.get(position).get("created_at"));
		tvsource.setText(lsHashData.get(position).get("source"));
		StatusTextProcess.setText(HomeActivity.this, tvstatustext, lsHashData.get(position).get("text"), lsHashData.get(position).get("id"), lsHashData.get(position).get("resource"));
		tvstatusreposts.setText(lsHashData.get(position).get("reposts_count"));
		tvstatuscomment.setText(lsHashData.get(position).get("comments_count"));
		tvstatusattitude.setText(lsHashData.get(position).get("attitudes_count"));
       
		String tid=((String)lsHashData.get(position).get("resource"))+"_"+((String)lsHashData.get(position).get("id"));
	    List<byte[]> tempList = mapImgList.get(tid);

		TableLayout tableLayout =(TableLayout)convertView.findViewById(R.id.tlPics);
		tableLayout.removeAllViews();
		if(tempList.size()>0)
		{
	    	WindowManager wm = ((Activity)context).getWindowManager();
	    	int width = wm.getDefaultDisplay().getWidth();
	    	
			TableRow row=null;
	    for(int i=0;i<tempList.size();i++)
		{
	    	if(i%3==0)
	    	{
	    		row=new TableRow(context);
	    		row.setGravity(Gravity.CENTER);
	    	}
	    	
			ImageView view = new ImageView(context);
			view.setPadding(5, 5, 5, 5);
			if(tempList.get(i)!=null)
			{

				Bitmap mBitmap =ImageFactory.scaleImg( width/3-10,  width/3-10, FormatTools.getInstance().Bytes2Bitmap(tempList.get(i)));
				
			//Drawable zoomDrawable=ModifyDrawableScale.zoomDrawable(FormatTools.getInstance().Bytes2Drawable(tempRetList.get(i)), ModifyDrawableScale.dip2px(context, width/3));
			//view.setImageDrawable(FormatTools.getInstance().Bytes2Drawable(tempRetList.get(i)));
			view.setImageBitmap(mBitmap);
             
			}
			else
			{
				view.setImageBitmap(loading);
			}
			row.addView(view);
			if((i%3==2)||(i==tempList.size()-1))
			{
				if(i==tempList.size()-1&&i%3!=2)
				{
					for(int j=0;j<(2-(i%3));j++)
					{
						ImageView view1 = new ImageView(context);
						row.addView(view1);
					}
				}
				tableLayout.addView(row);
			}
		}
		}
		
		tableLayout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(lsHashData.get(position).get("resource").equals("Renn"))
				{
					return;
				}
				Intent intent =new Intent(context,WeiBoDetailActivity.class);
				intent.putExtra("weiboID", lsHashData.get(position).get("id"));
				intent.putExtra("resource", lsHashData.get(position).get("resource"));
				context.startActivity(intent);
				
			}
		});
		
		
		if(lsHashData.get(position).get("retweeted_statustext")!=null)
		{
			tvretstatustext.setVisibility(View.VISIBLE);
			String content ="@"+lsHashData.get(position).get("retweeted_statususerscreen_name")+":"+lsHashData.get(position).get("retweeted_statustext");
			StatusTextProcess.setText(HomeActivity.this, tvretstatustext, content, lsHashData.get(position).get("retweeted_statusid"), lsHashData.get(position).get("resource"));
			
		}
		else
		{
			tvretstatustext.setVisibility(View.GONE);
		}
		if(lsHashData.get(position).get("resource").equals("QQ"))
		{
			ImageView ivResource=(ImageView)convertView.findViewById(R.id.ivResource); 
			ivResource.setImageResource(R.drawable.qq_icon);
		}
		else if(lsHashData.get(position).get("resource").equals("sina"))
		{
			ImageView ivResource=(ImageView)convertView.findViewById(R.id.ivResource); 
			ivResource.setImageResource(R.drawable.ic_com_sina_weibo_sdk_logo);
		}
		else if(lsHashData.get(position).get("resource").equals("netease"))
		{
			ImageView ivResource=(ImageView)convertView.findViewById(R.id.ivResource); 
			ivResource.setImageResource(R.drawable.net_logo);
		}
		else if(lsHashData.get(position).get("resource").equals("sohu"))
		{
			ImageView ivResource=(ImageView)convertView.findViewById(R.id.ivResource); 
			ivResource.setImageResource(R.drawable.sohu_logo);
		}
		tvstatusattitude.setOnClickListener(new TextView.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String attitude = tvstatusattitude.getText().toString();
				int sIndex=attitude.indexOf('（');
				int eIndex=attitude.indexOf('）');
				attitude=attitude.substring(sIndex+1,eIndex);
				attitude = Integer.parseInt(attitude)+1+"";
				attitude="赞（"+attitude+"）";
				tvstatusattitude.setText(attitude);
				
			}
		});
		tvstatuscomment.setOnClickListener(new TextView.OnClickListener() {

			
			@Override
			public void onClick(View arg0) {
				final View view=LayoutInflater.from(context).inflate
						(R.layout.dlg_comment,null );
				final AlertDialog dlg = new AlertDialog.Builder(((HomeActivity)context).getParent()).create();
				dlg.setView(view);
				Button btComment=(Button)view.findViewById(R.id.btComment);
				btComment.setOnClickListener(new Button.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						
						final EditText etContent=(EditText)view.findViewById(R.id.etCommentText);
						final CheckBox cbISRepost=(CheckBox)view.findViewById(R.id.cbIsRepost);
						String content =etContent.getText().toString() ;
						if(content.trim().equals(""))
						{
							Toast.makeText(context, "评论内容不能为空", 1000).show();
							return;
						}
						String comment =lsHashData.get(position).get("comments_count");
						int sIndex=comment.indexOf('（');
						int eIndex=comment.indexOf('）');
						comment=comment.substring(sIndex+1,eIndex);
						comment = Integer.parseInt(comment)+1+"";
						comment="评论（"+comment+"）";
						lsHashData.get(position).put("comments_count",comment);
						if(cbISRepost.isChecked() )
				    	{
							String repost =lsHashData.get(position).get("reposts_count");
							int sIndex1=repost.indexOf('（');
							int eIndex1=repost.indexOf('）');
							repost=repost.substring(sIndex1+1,eIndex1);
							repost = Integer.parseInt(repost)+1+"";
							repost="评论（"+repost+"）";
							lsHashData.get(position).put("reposts_count",repost);
				    	}
						adapter.notifyDataSetChanged();
						if(lsHashData.get(position).get("resource").equals("sina"))
						{
							if(cbISRepost.isChecked() )
					    	{
					    	SinaAPI.repost(handler ,Long.parseLong(lsHashData.get(position).get("id")), content, false);
						    }
						    SinaAPI.comment(handler, Long.parseLong(lsHashData.get(position).get("id")), content);
						}
						else if(lsHashData.get(position).get("resource").equals("QQ"))
						{
							if(cbISRepost.isChecked() )
					    	{
					    	QQAPI.repost(context ,lsHashData.get(position).get("id"), content,handler);
						    }
							else
							{
						     QQAPI.comment(context, lsHashData.get(position).get("id"), content,handler);
							}
						}
						else if(lsHashData.get(position).get("resource").equals("Renn"))
						{
							if(cbISRepost.isChecked() )
					    	{
								 RennAPI.comment(handler,content,Long.parseLong(lsHashData.get(position).get("id")));
					    	RennAPI.share(handler,content,Long.parseLong(lsHashData.get(position).get("id")));
						    }
							else
							{
						     RennAPI.comment(handler,content,Long.parseLong(lsHashData.get(position).get("id")));
							}
							}
						else if(lsHashData.get(position).get("resource").equals("netease"))
						{
							if(cbISRepost.isChecked())
						    {
								NetAPI.comment(content, lsHashData.get(position).get("id"), "1", handler);
						    }
							else
							{
								NetAPI.comment(content, lsHashData.get(position).get("id"), "0", handler);
							}
						}
						else if(lsHashData.get(position).get("resource").equals("sohu"))
						{
							if(cbISRepost.isChecked())
						    {
								SohuWeiboAPI.repost(content, lsHashData.get(position).get("id"), handler);
						    }
							SohuWeiboAPI.comment(content,  lsHashData.get(position).get("id"), handler);
						}
						dlg.dismiss();
					}
				});
                Button btCancel=(Button)view.findViewById(R.id.btCancel);
				
				btCancel.setOnClickListener(new Button.OnClickListener() {
					
					@Override
					public void onClick(View v) {
					dlg.cancel();
					}
				});
			    dlg.show();
			}
			
		}) ;

		
        tvstatusreposts.setOnClickListener(new TextView.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(context,  "the "+position+"th repost click",1000).show();
				View view=LayoutInflater.from(context).inflate
						(R.layout.dlg_repost,null );
				TextView tvSourceUser=(TextView)view.findViewById(R.id.tvsourceuser);
				final EditText etContent=(EditText)view.findViewById(R.id.etRepostText);
				final CheckBox cbISComment=(CheckBox)view.findViewById(R.id.cbIsComment);
				
				final AlertDialog dlg = new AlertDialog.Builder(((HomeActivity)context).getParent()).create();
				dlg.setView(view);
				
				
				Button btRepost=(Button)view.findViewById(R.id.btRepost);
				
				btRepost.setOnClickListener(new Button.OnClickListener() {
					
					@Override
					public void onClick(View v) {
					String content =etContent.getText().toString() ;
					String repost =lsHashData.get(position).get("reposts_count");
					int sIndex1=repost.indexOf('（');
					int eIndex1=repost.indexOf('）');
					repost=repost.substring(sIndex1+1,eIndex1);
					repost = Integer.parseInt(repost)+1+"";
					repost="评论（"+repost+"）";
					lsHashData.get(position).put("reposts_count",repost);
					boolean isComment =cbISComment.isChecked();
					if(isComment)
					{
						String comment =lsHashData.get(position).get("comments_count");
						int sIndex=comment.indexOf('（');
						int eIndex=comment.indexOf('）');
						comment=comment.substring(sIndex+1,eIndex);
						comment = Integer.parseInt(comment)+1+"";
						comment="评论（"+comment+"）";
						lsHashData.get(position).put("comments_count",comment);
					}
					adapter.notifyDataSetChanged();
					if(lsHashData.get(position).get("resource").equals("sina"))
					{
					SinaAPI.repost(handler ,Long.parseLong(lsHashData.get(position).get("id")), content, isComment);
					}
					else if(lsHashData.get(position).get("resource").equals("QQ"))
					{
						QQAPI.repost(context, lsHashData.get(position).get("id"), content,handler);
					}
					else if(lsHashData.get(position).get("resource").equals("Renn"))
					{
						RennAPI.share(handler, content, Long.parseLong(lsHashData.get(position).get("id")));
						if(isComment)
							RennAPI.comment(handler, content, Long.parseLong(lsHashData.get(position).get("id")));
					}
					else if(lsHashData.get(position).get("resource").equals("netease"))
					{
						if(isComment)
					    {
							NetAPI.repost(content, lsHashData.get(position).get("id"), "1", handler);
					    }
						else
						{
							NetAPI.repost(content, lsHashData.get(position).get("id"), "0", handler);
						}
					}
					else if(lsHashData.get(position).get("resource").equals("sohu"))
					{
						SohuWeiboAPI.repost(content, lsHashData.get(position).get("id"), handler);
						if(isComment)
					    {
							SohuWeiboAPI.comment(content, lsHashData.get(position).get("id"), handler);
					    }
					}
					dlg.cancel();
					}
				});
                Button btCancel=(Button)view.findViewById(R.id.btCancel);
				
				btCancel.setOnClickListener(new Button.OnClickListener() {
					
					@Override
					public void onClick(View v) {
					dlg.cancel();
					}
				});
				dlg.show();
				tvSourceUser.setText(lsHashData.get(position).get("userscreen_name"));
				
			}
		});
		}
		
		return convertView;
	}
	 
}

	
	
}



